package com.lagou.controller;

import com.lagou.constant.OrderStatusConstant;
import com.lagou.mqsender.dlx.DLXNormalSender;
import com.lagou.service.PayOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * @Description:
 * @Author: qjc
 * @Date: 2021/12/9
 */
@RestController
@CrossOrigin(origins = "*", methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE, RequestMethod.PUT})
public class MqSenderController {


    @Resource
    DLXNormalSender dlxNormalSender;
    @Autowired
    private PayOrderService payOrderService;


    /**
     * http://localhost:8899/create/order
     * 创建订单
     */
    @RequestMapping(value = "/create/order", method = {RequestMethod.GET})
    public String createOrder() {
        // 下单成功等待付款，10s有效期
        Integer seconds = 10;
        return dlxNormalSender.send(seconds);
    }

    /**
     * http://localhost:8899/pay/order/xxxxx
     * 支付订单
     */
    @RequestMapping(value = "/pay/order/{orderNo}", method = {RequestMethod.GET})
    public String payOrder(@PathVariable String orderNo) {
        String orderStatus = payOrderService.getOrderStatus(orderNo);
        if (StringUtils.isEmpty(orderStatus)) {
            return "该订单不存在，请先下单";
        }
        if (OrderStatusConstant.CANCEL.equals(orderStatus)) {
            return "订单已取消，请重新下单！";
        }
        payOrderService.updateOrderStatus(orderNo, OrderStatusConstant.PAID);
        return "支付成功，跳转到支付成功页面";
    }

    /**
     * http://localhost:8899/find/order/03113c64a12346328d2fa02b11d6a372
     * 查询订单
     */
    @RequestMapping(value = "/find/order/{orderNo}", method = {RequestMethod.GET})
    public String findOrder(@PathVariable String orderNo) {
        String orderStatus = payOrderService.getOrderStatus(orderNo);
        if (StringUtils.isEmpty(orderStatus)) {
            return "该订单不存在，请先下单";
        }
        return "订单编号为" + orderNo + "的订单状态为：" + orderStatus;
    }

}
